﻿using System;
using System.Data;
using System.Reflection;
using System.Collections.Generic;
using System.Globalization;

namespace SiteUnity.Frame.Common.Tools.Class
{
    public class ClassMapper
    {
        public static T ToClass<T>(DataRow dataRow)
        {
            PropertyInfo[] properties = typeof(T).GetProperties();

            T result = Activator.CreateInstance<T>();

            foreach (PropertyInfo property in properties)
            { 
                if (dataRow.Table.Columns.Contains(property.Name))
                   ClassTools.SetProperty(result, property, dataRow[property.Name] is DBNull ? null : dataRow[property.Name]);
            }

            return result;
        }

        public static T ToClass<T>(DataRow dataRow, PropertyInfo[] properties)
        {
            T result = Activator.CreateInstance<T>();

            foreach (PropertyInfo property in properties)
            {
                if (dataRow.Table.Columns.Contains(property.Name) && !(dataRow[property.Name] is DBNull))
                    ClassTools.SetProperty(result, property, dataRow[property.Name] is DBNull ? null : dataRow[property.Name]);
            }

            return result;
        }

        public static List<T> ToClassList<T>(DataTable data)
        {
            PropertyInfo[] properties = typeof(T).GetProperties();

            List<T> result = new List<T>();
            foreach (DataRow dataRow in data.Rows)
            {
                result.Add(ToClass<T>(dataRow, properties));
            }

            return result;
        }

        public static DataTable ToDataColumn<T>(PropertyInfo[] properties)
        {
            DataTable result = new DataTable();
            foreach (PropertyInfo property in properties)
            {
                DataColumn column = new DataColumn(property.Name, property.PropertyType);
                result.Columns.Add(column);
            }

            return result;
        }

        public static DataTable ToDataColumn<T>()
        {
            PropertyInfo[] properties = typeof(T).GetProperties();

            return ToDataColumn<T>(properties);
        }

        public static DataTable ToDataTable<T>(List<T> items)
        {
            PropertyInfo[] properties = typeof(T).GetProperties();

            DataTable result = ToDataColumn<T>(properties);

            foreach (T item in items)
                result.Rows.Add(ToDataRow<T>(item, result, properties));

            return result;
        }

        public static DataRow ToDataRow<T>(T item, DataTable datatable)
        {
            PropertyInfo[] properties = typeof(T).GetProperties();

            return ToDataRow<T>(item, datatable,properties);
        }

        public static DataRow ToDataRow<T>(T item, DataTable datatable, PropertyInfo[] properties)
        {
            DataRow dataRow = datatable.NewRow();
            foreach (PropertyInfo property in properties)
            {
                dataRow[property.Name] = ClassTools.GetProperty(item, property);
            }

            return dataRow;
        }
    }
}
